/**
 * Created by LaoXu on 2017/12/21.
 * @description:用户信息
 */
const pool = require('./base/pool');
const codeCons = require('./constants/codeCons');
class User {
    constructor() {

    }

    /**
     * 根据手机号码获取用户信息
     * @param params
     * @param callback
     */
    getUserInfoByMobile(params, callback) {
        callback = callback || function () {
            };
        var {mobile, password} = params;
        if (!mobile) {
            callback(codeCons.createCodeObj(codeCons.PARAMS_EXCPTION, '请检查您传递的参数'));
        } else {
            pool.execSQL(`SELECT * FROM t_user WHERE uid = ?`, [mobile], function (results) {
                if (results.code == 0) {
                    var userInfo = results.datas[0];
                    if (userInfo) { // 查询到用户信息
                        callback(codeCons.createCodeObj(codeCons.SUCCESSFUL, userInfo));
                    } else { // 没有查询到用户信息
                        callback(codeCons.createCodeObj(codeCons.NO_DATA, '用户信息未找到'));
                    }
                } else {
                    callback(results);
                }
            });
        }
    }

    /**
     * 根据用户手机号码获取用户信息，包含登录
     * @param params
     * @param callback
     */
    getUserByMobile(params, callback) {
        callback = callback || function () {
            };
        var {mobile, password} = params;
        if (!mobile || !password) {
            callback(codeCons.createCodeObj(codeCons.PARAMS_EXCPTION, '请检查您传递的参数'));
        } else {
            this.getUserInfoByMobile(params, function (results) {
                if (password == results.datas.password) {
                    callback(codeCons.createCodeObj(codeCons.SUCCESSFUL, results.datas));
                } else {
                    callback(codeCons.createCodeObj(codeCons.FAILED, '用户名或密码错误'));
                }
            })
        }
    }

    userLogin(params, callback) {
        callback = callback || function () {
            };
        var {mobile, password} = params;
        if (!mobile || !password) {
            callback(codeCons.createCodeObj(codeCons.PARAMS_EXCPTION, '请检查您传递的参数'));
        } else {
            this.getUserByMobile(params, callback);
        }
    }

    /**
     * 插入用户信息
     * @param params
     * @param callback
     */
    insertUser(params, callback) {
        callback = callback || function () {
            };
        this.getUserInfoByMobile(params, function (results) {
            if (results.code === codeCons.NO_DATA) {
                insertUserInfo(params, callback);
            } else {
                callback(codeCons.createCodeObj(codeCons.FAILED, '用户已存在'));
            }
        })
    }
}

/**
 * 插入用户信息
 * @param params
 * @param callback
 */
function insertUserInfo(params, callback) {
    callback = callback || function () {
        };
    var {mobile, userName, password} = params;
    if (!mobile || !userName || !password) {
        callback(codeCons.createCodeObj(codeCons.PARAMS_EXCPTION, '请检查您传递的参数'));
    } else {
        pool.execSQL(`INSERT INTO t_user(uid,user_name,PASSWORD) VALUES(?,?,?)`, [mobile, userName, password], function (results) {
            callback(results);
        });
    }
}

module.exports = User;
